import zlib
import base64


# 压缩基因数据
def compress_gene_data(gene_data):
    compressed_data = zlib.compress(gene_data.encode('utf-8'))
    compression_ratio = len(compressed_data) / len(gene_data)
    return compressed_data, compression_ratio


# 解压缩基因数据
def decompress_gene_data(compressed_data):
    gene_data = zlib.decompress(compressed_data).decode('utf-8')
    return gene_data


if __name__ == "__main__":
    # 读取基因数据文件
    with open("gene_data.txt", "r") as f:
        gene_data = f.read().strip()

    # 压缩基因数据
    compressed_data, compression_ratio = compress_gene_data(gene_data)
    print("Compression ratio:", compression_ratio)

    # 将压缩数据转换为字符串类型
    compressed_data_str = base64.b64encode(compressed_data).decode('utf-8')

    # 写入压缩数据到文件
    with open("compressed_gene_data.txt", "w") as f:
        f.write(compressed_data_str)

    # 解压缩基因数据
    compressed_data_bytes = base64.b64decode(compressed_data_str)
    decompressed_data = decompress_gene_data(compressed_data_bytes)

    # 将解压缩后的基因数据写入文件
    with open("decompressed_gene_data.txt", "w") as f:
        f.write(decompressed_data)
